<?php

require_once("model/routeCollection.class.php");
require_once("model/route.class.php");
require_once("model/place.class.php");

class predefRoutes{


	static public function getRoute($id, $date){
		//	$collectionTab = array();
		switch($id){
			case 1:		// Oslo - Bergen
			case 2:		// Bergen - Oslo
				$collection = new routeCollection("Oslo", "Bergen");
				$route1 = new RouteModel("Oslo", "Bergen", $date);
				$route1->setName("Filefjell");
				$route1->setRouteNumber("16");
				$route1->setRouteType("Ev");
				$route1->addPlace(placeFromSql::findPlaceName("Oslo"),0, $date);
				//	$route1->addPlace(placeFromSql::findPlaceName("Ringerike"),56.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Fagernes"),184.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Vang"),236.0, $date);
				//	$route1->addPlace(placeFromSql::findPlaceName("Borgund"),289.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Aurland"),343.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Voss"),416.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Bergen"),518.0, $date);
				$route1->setTotalDist(518.0);
				$route2 = new RouteModel("Oslo", "Bergen", $date);
				$route2->setName("Hemsedal");
				$route2->setRouteNumber("52");
				$route2->setRouteType("Rv");
				$route2->addPlace(placeFromSql::findPlaceName("Oslo"),0,$date);
				//	$route2->addPlace(placeFromSql::findPlaceName("Ringerike"),56.0,$date);
				$route2->addPlace(placeFromSql::findPlaceName("Nesbyen"),166.5,$date);
				$route2->addPlace(placeFromSql::findPlaceName("Hemsedal"),219.0,$date);
				//	$route2->addPlace(placeFromSql::findPlaceName("Borgund"),269.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Aurland"),324.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Voss"),397.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Bergen"),497.0, $date);
				$route2->setTotalDist(497.0);
				$route3 = new RouteModel("Oslo", "Bergen", $date);
				$route3->setName("Hol-Aurland");
				$route3->setRouteNumber("50");
				$route3->setRouteType("Rv");				
				
				
				$route3->addPlace(placeFromSql::findPlaceName("Oslo"),0,$date);
				//	$route3->addPlace(placeFromSql::findPlaceName("Ringerike"),56.0,$date);
				$route3->addPlace(placeFromSql::findPlaceName("Nesbyen"),166.5,$date);
				$route3->addPlace(placeFromSql::findPlaceName("Hol"),232.0,$date);

				$route3->addPlace(placeFromSql::findPlaceName("Aurland"),323.0, $date);
				$route3->addPlace(placeFromSql::findPlaceName("Voss"),396.0, $date);
				$route3->addPlace(placeFromSql::findPlaceName("Bergen"),496.0, $date);
				$route3->setTotalDist(496.4);
				$route4 = new RouteModel("Oslo", "Bergen", $date);
				$route4->setName("Hardangervidda");
				$route4->setRouteNumber("7");
				$route4->setRouteType("Rv");
				$route4->addPlace(placeFromSql::findPlaceName("Oslo"),0,$date);
				//	$route4->addPlace(placeFromSql::findPlaceName("Ringerike"),56.0,$date);
				$route4->addPlace(placeFromSql::findPlaceName("Nesbyen"),166.5,$date);
				$route4->addPlace(placeFromSql::findPlaceName("Geilo"),239.0,$date);

				$route4->addPlace(placeFromSql::findPlaceName("Eidfjord"),329.0, $date);
				//		$route4->addPlace(placeFromSql::findPlaceName("Granvin"),353.0, $date);
				$route4->addPlace(placeFromSql::findPlaceName("Voss"),378.0, $date);
				$route4->addPlace(placeFromSql::findPlaceName("Bergen"),478.0, $date);
				$route4->setTotalDist(478.0);
				$collection->addRoute($route1);
				$collection->addRoute($route2);
				$collection->addRoute($route3);
				$collection->addRoute($route4);

				predefRoutes::getWeather($collection, $id);
				return $collection;
				break;
			case 3:
			case 4:
				$collection = new routeCollection("Bergen", "Stavanger");
				$route1 = new RouteModel("Bergen", "Stavanger", $date);
				$route1->setName("");
				$route1->setRouteNumber("39");
				$route1->setRouteType("Ev");
				$route1->addPlace(placeFromSql::findPlaceName("Bergen"),0, $date);
					
				$route1->addPlace(placeFromSql::findPlaceName("Os"),28.6, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Stord"),82.1, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Bokn"),161.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Stavanger"),207.0, $date);
				$route1->setTotalDist(207.0);
				$collection->addRoute($route1);

				predefRoutes::getWeather($collection, $id);
				return $collection;
				break;
			case 5:
			case 6:
				$collection = new routeCollection("Oslo", "Trondheim");
				$route1 = new RouteModel("Oslo", "Trondheim", $date);
				$route1->setName("Gudbrandsdalen");
				$route1->setRouteNumber("6");
				$route1->setRouteType("Ev");
				$route1->addPlace(placeFromSql::findPlaceName("Oslo"),0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Hamar"),126.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Ringebu"),238.1, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Dovre"),328.0, $date);
				//	$route1->addPlace(placeFromSql::findPlaceName("Oppdal"),418.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Trondheim"),538.0, $date);
				$route1->setTotalDist(538.0);

				$route2 = new RouteModel("Oslo", "Trondheim", $date);
				$route2->setName("Østerdalen");
				$route2->setRouteNumber("3");
				$route2->setRouteType("Rv");
				$route2->addPlace(placeFromSql::findPlaceName("Oslo"),0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Elverum"),141.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Stor-Elvdal"),218.1, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Tynset"),328.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Trondheim"),495.0, $date);
				$route2->setTotalDist(495.0);
				$collection->addRoute($route1);
				$collection->addRoute($route2);

				predefRoutes::getWeather($collection, $id);
				return $collection;
				break;
					
			case 7:
			case 8:
				$collection = new routeCollection("Oslo", "Stavanger");
				$route1 = new RouteModel("Oslo", "Stavanger", $date);
				$route1->setName("Kristiansand");
					$route1->setRouteNumber("18");
				$route1->setRouteType("Ev");
				$route1->addPlace(placeFromSql::findPlaceName("Oslo"),0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Larvik"),134.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Arendal"),257.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Kristiansand"),325.1, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Flekkefjord"),432.0, $date);
				$route1->addPlace(placeFromSql::findPlaceName("Stavanger"),557.0, $date);
				$route1->setTotalDist(557.0);

				$route2 = new RouteModel("Oslo", "Trondheim", $date);
				$route2->setName("Haukeli");
					$route2->setRouteNumber("134");
				$route2->setRouteType("Ev");
				$route2->addPlace(placeFromSql::findPlaceName("Oslo"),0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Notodden"),111.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Vinje"),270.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Suldal"),395.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Tau"),475.0, $date);
				$route2->addPlace(placeFromSql::findPlaceName("Stavanger"),493.0, $date);
				$route2->setTotalDist(493.0);
				$collection->addRoute($route1);
				$collection->addRoute($route2);

				predefRoutes::getWeather($collection, $id);
				return $collection;
				break;

			default:
				die("feil i getRoute(), storage/predefRoutes.php");
		}
	}

	private static function getWeather($collection, $id){
		if (($id % 2) === 0){
			predefRoutes::reverse($collection);
		} else {
			$arr = $collection->getAllRoutes();
			for ($n=0; $n<count($arr); $n++){
				$arr[$n]->generateForecast();
			}
		}
	}

	private static function reverse($collection){
		$collection->reverse();
		$arr = $collection->getAllRoutes();
		for ($n=0; $n<count($arr); $n++){
			$array = $arr[$n]->getPlaceArray();
			for ($i=0; $i<count($array); $i++){
				$place = $array[$i];
				$newDist = ($arr[$n]->getTotalDist() - $place->getDistance() );
				$place->setDistance($newDist);
				$place->generateDate($arr[$n]->getStartDate(), $newDist);
			}
			$arr[$n]->generateForecast();
		}
	}

}

?>
